package com._119yn.host.gst200.utils;


import com._119yn.host.gst200.database.entity.PointTableRecord;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class PointTableRecordUtils {

    public static final Set<String> CONTROLLER_TYPES = Set.of("aa", "bb");

    public static final Set<String> DTU_TYPES = Set.of("dtu");

    public static boolean isController(PointTableRecord pointTableRecord) {
        return pointTableRecord != null && pointTableRecord.getDeviceType() != null && CONTROLLER_TYPES.contains(pointTableRecord.getDeviceType());
    }

    public static boolean isDtu(PointTableRecord pointTableRecord) {
        return pointTableRecord != null && pointTableRecord.getDeviceType() != null && DTU_TYPES.contains(pointTableRecord.getDeviceType());
    }

    public static List<PointTableRecord> parse(InputStream inputStream, Charset fileCharset) throws IOException {
        List<PointTableRecord> pointTableRecords = new LinkedList<>();
        CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().setSkipHeaderRecord(true).get();
        try (CSVParser parser = CSVParser.parse(inputStream, fileCharset, format)) {
            for (CSVRecord csvRecord : parser) {
                PointTableRecord pointTableRecord = new PointTableRecord();
                pointTableRecord.setSn(Integer.parseInt(csvRecord.get("序号")));
                pointTableRecord.setDeviceAddress(StringUtils.strip(csvRecord.get("设备地址")));
                pointTableRecord.setDeviceId(StringUtils.strip(csvRecord.get("设备ID")));
                pointTableRecord.setDeviceType(StringUtils.strip(csvRecord.get("设备类型")));
                pointTableRecord.setText(StringUtils.strip(csvRecord.get("文本信息")));
                switch (pointTableRecord.getDeviceType()) {
                    // 火灾显示盘
                    case "40":
                        // 声光
                    case "76":
                        // 火警传输
                    case "77":
                        // 多线制盘
                    case "87":
                        // 报警总线
                    case "90":
                        // 存储故障
                    case "93":
                        // 接地故障
                    case "94":
                        // 手动盘
                    case "96":
                        // 交流电源
                    case "97":
                        // 备用电源
                    case "98":
                        // 键盘
                    case "99":
                        // 控制器
                    case "aa": {
                        String[] split = pointTableRecord.getDeviceId().split("_");
                        int panel = Integer.parseInt(StringUtils.stripStart(split[0], "panel"));
                        pointTableRecord.setPanel(panel);
                        pointTableRecord.setLoop(0);
                        pointTableRecord.setCode(0);
                        pointTableRecords.add(pointTableRecord);
                        break;
                    }
                    // DTU
                    case "dtu": {
                        pointTableRecord.setDeviceAddress("9999");
                        pointTableRecord.setPanel(0);
                        pointTableRecord.setLoop(0);
                        pointTableRecord.setCode(0);
                        pointTableRecords.add(pointTableRecord);
                        break;
                    }
                    default: {
                        String[] split = pointTableRecord.getDeviceId().split("_");
                        int panel = Integer.parseInt(StringUtils.stripStart(split[0], "panel"));
                        int loop = Integer.parseInt(StringUtils.stripStart(split[1], "lp"));
                        int code = Integer.parseInt(StringUtils.stripStart(split[2], "dev"));
                        pointTableRecord.setPanel(panel);
                        pointTableRecord.setLoop(loop);
                        pointTableRecord.setCode(code);
                        pointTableRecords.add(pointTableRecord);
                    }
                }
            }
        }
        return pointTableRecords;
    }
}
